package com.torry.workflow.service.web.controller;

import com.torry.workflow.api.user.api.RoleApi;
import com.torry.workflow.api.user.api.UserApi;
import com.torry.workflow.api.user.dto.UserPageDTO;
import com.torry.workflow.api.user.entity.User;
import com.torry.workflow.common.base.result.LoginResult;
import com.torry.workflow.common.base.result.Result;
import com.torry.workflow.common.util.HttpContextUtil;
import com.torry.workflow.common.util.JwtUtil;
import io.swagger.annotations.Api;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/user")
@RestController
@Api(tags = "用户接口")
public class UserController {
    @DubboReference
    private UserApi userApi;
    @DubboReference
    private RoleApi roleApi;

    @PostMapping("login")
    public Result login(@RequestParam String username, @RequestParam String password) {
        LoginResult loginResult = userApi.login(username, password);
        return Result.success("登录成功", loginResult);
    }

    @GetMapping("info")
    public Result info() {
        String token = HttpContextUtil.getRequestToken();
        Integer userId = JwtUtil.getUserIdFromToken(token);
        User userInfo = userApi.getUserInfo(userId);
        return Result.success("查询用户信息成功", userInfo);

    }

    @PostMapping("list")
    public Result userList(@RequestBody UserPageDTO userPageDTO) {
        return Result.selectPageSuccess(userApi.userList(userPageDTO));
    }

    @PutMapping("updateStatus/{userId}/{status}")
    public Result updateStatus(@PathVariable Integer userId, @PathVariable Integer status) {
        if (status == null) {
            status = 0;
        }
        userApi.updateStatus(userId, status);
        return Result.success();
    }

    @PostMapping("saveUserInfo")
    public Result updateStatus(@RequestBody User user) {

        userApi.saveUser(user);
        return Result.saveSuccess();
    }

    @DeleteMapping("delete/{id}")
    public Result updateStatus(@PathVariable Integer id) {

        userApi.deleteUserById(id);
        return Result.delSuccess();
    }

    @GetMapping("allUsers")
    public Result allUsers() {
        List<User> users = userApi.allUsers();
        return Result.selectSuccess(users);
    }
}
